package com.java.example.ui.dialog

import android.app.AlertDialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.Gravity
import android.view.View
import android.view.WindowManager
import com.java.example.R

class BleDisconnectDialog : View.OnClickListener {
    private lateinit var mDialog: AlertDialog

    fun showDialog(context: Context?) {
        mDialog = AlertDialog.Builder(context, R.style.normal_dialog).create()
        mDialog.setCancelable(true)
        mDialog.setCanceledOnTouchOutside(true)
        mDialog.show()
        val window = mDialog.window
        if (window != null) {
            window.setContentView(R.layout.dialog_ble_disconnect)
            window.setGravity(Gravity.CENTER)
            window.setWindowAnimations(R.style.anim_panel_up_from_bottom)
            window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
            //设置属性
            val params = window.attributes
            params.width = WindowManager.LayoutParams.MATCH_PARENT
            params.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND
            params.dimAmount = 0.5f
            window.attributes = params
            window.findViewById<View>(R.id.tv_sure).setOnClickListener(this)
        }
    }

    fun dismiss() {
        if (::mDialog.isInitialized) {
            mDialog.dismiss()
        }
    }

    override fun onClick(v: View) {
        if (v.id == R.id.tv_sure) {
            if (::mDialog.isInitialized) {
                mDialog.dismiss()
            }
        }
    }

    companion object {
        fun create(): BleDisconnectDialog {
            return BleDisconnectDialog()
        }
    }
}
